from pyglet.gl import *
import pyglet
from pyglet.window import *
"""
When you draw on a window in pyglet, you are drawing to an OpenGL context. Every window has its own context, which is created when the window is created. You can access the window's context via its context attribute.
"""

window = pyglet.window.Window(width=640, height=480, resizable=True)

rtri = 0.0	# Angle for the triangle
rquad = 0.0	# Angle for the quad

@window.event
def on_key_press(symbol, modifiers):
  if symbol == key.RETURN:
		exit()

def update(dt):
  	global rtri, rquad

	# Spin the objects with a constant speed
	rtri += 50*dt
	rquad += 40*dt

pyglet.clock.schedule(update)

@window.event
def on_resize(width, height):
	if height==0:
		height = 1
	glViewport(0, 0, width, height)

	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()

	# Calculate the aspect ratio of the window
	gluPerspective(45.0, 1.0*width/height, 0.1, 100.0)

	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	return pyglet.event.EVENT_HANDLED

	
def init():
  	"""
	Pyglet oftentimes calls this setup()	
	"""
  	glShadeModel(GL_SMOOTH)	# Enables smooth shading
	glClearColor(0.0, 0.0, 0.0, 0.0) #Black background
	
	glClearDepth(1.0)		# Depth buffer setup
	glEnable(GL_DEPTH_TEST)		# Enables depth testing
	glDepthFunc(GL_LEQUAL)		# The type of depth test to do

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)	# Really nice perspective calculations


@window.event
def on_draw():
  	# Here we do all the drawing
	glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)
	glLoadIdentity()	

	glTranslatef(-1.5, 0.0, -6.0) # Move left 1.5 units and into the screen 6.0
	
	glRotatef(rtri, 0.0, 1.0, 0.0)	# rotate triangle on the y axis

	red = (1.0, 0.0, 0.0)
	green = (0.0, 1.0, 0.0)
	blue = (0.0, 0.0, 1.0)
	orange = (1.0, 0.5, 0.0)
	yellow = (1.0, 1.0, 0.0)
	violet=(1.0, 0.0, 1.0)

	top = (0.0, 1.0, 0.)

	glBegin(GL_TRIANGLES)
	# Draw in countre-clockwise dir
	glColor3f(*red)
	glVertex3f(*top)	# top of triangle (front)
	glColor3f(*green)
	glVertex3f(-1.0, -1.0, 1.0)	# left of triangle (front)
	glColor3f(*blue)
	glVertex3f(1.0, -1.0, 1.0)	# right of triangle (front)

	
	glColor3f(*red)
	glVertex3f(*top)	# top of triangle (right)
	glColor3f(*blue)
	glVertex3f(1.0, -1.0, 1.0)	# left of triangle (right)
	glColor3f(*green)
	glVertex3f(1.0, -1.0, -1.0)	# right of triangle (right)

	# back
	glColor3f(*red)
	glVertex3f(*top)	# top
	glColor3f(*green)
	glVertex3f(1.0, -1.0, -1.0)	#left
	glColor3f(*blue)
	glVertex3f(-1.0, -1.0, -1.0)	#right

	# left
	glColor3f(*red)
	glVertex3f(*top)	# top of triangle (left)
	glColor3f(*blue)
	glVertex3f(-1.0, -1.0, -1.0)	# left of triange (left)
	glColor3f(*green)
	glVertex3f(-1.0, -1.0, 1.0)	# right of triangle (left)

	glEnd()

	glLoadIdentity()
	glTranslatef(1.5, 0.0, -7.0) # Move right 1.5 units and into the screen

	glRotatef(rquad, 1.0, 1.0, 1.0) # rotate cube on x,y & z!

	# Draw a cube
	glBegin(GL_QUADS)

	# Top face
	glColor3f(*green)
	glVertex3f(1.0, 1.0, -1.0)	#top right
	glVertex3f(-1.0, 1.0, -1.0)	#top left
	glVertex3f(-1.0, 1.0, 1.0)	#bottom left
	glVertex3f(1.0, 1.0, 1.0)		# bottom right

	# Bottom face
	glColor3f(*orange)
	v3(1, -1, 1)
	v3(-1, -1, 1)
	v3(-1, -1, -1)
	v3(1, -1, -1)

	# Front face
	glColor3f(*red)
	m3([ [1,   1, 1],
	  	 [-1,  1, 1],
		 [-1, -1, 1],
		 [1,  -1, 1]])

	# Back
	glColor3f(*yellow)
	m3([ [1, -1, -1],
	  	[-1, -1, -1],
		[-1, 1,-1],
		[1,1,-1]])

	# Left
	glColor3f(*blue)
	m3([[-1, 1, 1],
	  	[-1,1,-1],
		[-1,-1,-1],
		[-1,-1,1]])

	# right
	glColor3f(*violet)
	m3([[1,1,-1], [1,1,1], [1,-1,1], [1,-1,-1]])


	glEnd()

	return pyglet.event.EVENT_HANDLED
  	
def m3(matrix):
  for row in matrix:
	v3(*row)
	
def v3(x,y,z):
  glVertex3f( float(x), float(y), float(z))

init()	

pyglet.app.run()
